package fy.uap.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;

import fy.config.FyConfig;
import fy.uap.bean.SupplierBean;
import fy.util.DBUtil;
import fy.util.MyJSONConvert;


public class ProviderInfoOperateDao {
	private static org.apache.commons.logging.Log log = LogFactory
			.getLog(ProviderInfoOperateDao.class);

	private final static String ERROR_QUERY_INFO = "{success : false, data : '查询发生异常，请重试，或者联系OA系统管理员。'}";
	private final static String WARN_QUERY_INFO_NULL = "{success : false, data : '未查询到该供应商，原因：\\n1、该供应商不存在；\\n2、该供应商非用户所在子公司的潜在/合格供应商'}";

	public static String queryProviderInfoMPI(String code, String corp) {
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		String result = null;
		try {
			conn = DBUtil.connect(FyConfig.getUAP_DB_CONFIG());


			pst = conn
					.prepareStatement("select i.bankaccountname, i.bankcode, i.bank, i.bankaccount, i.currency, v.* from fy_supplier_vendor_invclass i left join fy_supplier_vendor v on i.pk_vendor = v.pk_vendor where rownum = 1 and v.cvencode=? and i.pk_corp = ? and nvl(i.dr,0) = 0 and nvl(v.dr,0) = 0 ");
			pst.setString(1, code);
			pst.setString(2, corp);

			rs = pst.executeQuery();

			if (rs.next()) {

				ResultSetMetaData md = rs.getMetaData();
				SupplierBean bean = new SupplierBean();
				// Map<String, Object> values = new HashMap<String, Object>();
				for (int i = 0, count = md.getColumnCount(); i < count; i++) {
					String name = md.getColumnName(i + 1);
					if ("cvenname".toUpperCase().equals(name)) {
						bean.setProviderName(rs.getString(i + 1));
					} else if ("cvencode".toUpperCase().equals(name)) {
						bean.setProviderCode(rs.getString(i + 1));
					} else if ("cveniaddress".toUpperCase().equals(name)) {
						bean.setAddress(rs.getString(i + 1));
					} else if ("cvenperson".toUpperCase().equals(name)) {
						bean.setLinkman(rs.getString(i + 1));
					} else if ("cycle".toUpperCase().equals(name)) {
						bean.setSupplyCycle(rs.getString(i + 1));
					} else if ("cvenbank".toUpperCase().equals(name)) {
						bean.setCvenbank(rs.getString(i + 1));
					} else if ("cvenaddress".toUpperCase().equals(name)) {
						bean.setSupplyAddress(rs.getString(i + 1));
					} else if ("CVENPHONE".toUpperCase().equals(name)) {
						bean.setTel(rs.getString(i + 1));
					} else if ("cpurbillcode".toUpperCase().equals(name)) {
						bean.setCpurbillcode(rs.getString(i + 1));
					} else if ("cvenregcode".toUpperCase().equals(name)) {
						bean.setTaxRegister(rs.getString(i + 1));
					} else if ("cvenfax".toUpperCase().equals(name)) {
						bean.setFax(rs.getString(i + 1));
					} else if ("couttype".toUpperCase().equals(name)) {
						bean.setCouttype(rs.getString(i + 1));
					} else if ("cmoneytype".toUpperCase().equals(name)) {
						bean.setCmoneytype(rs.getString(i + 1));
					} else if ("cqualitycond".toUpperCase().equals(name)) {
						bean.setQualitySystem(rs.getString(i + 1));
					} else if ("cvenpostcode".toUpperCase().equals(name)) {
						bean.setPostCode(rs.getString(i + 1));
					} else if ("ctrade".toUpperCase().equals(name)) {
						bean.setCompanyType(rs.getString(i + 1));
					} else if ("ccuspaycond".toUpperCase().equals(name)) {
						bean.setCcuspaycond(rs.getString(i + 1));
					} else if ("dorf".toUpperCase().equals(name)) {
						bean.setProperty(rs.getString(i + 1));
					} else if ("cvenemail".toUpperCase().equals(name)) {
						bean.setEmail(rs.getString(i + 1));
					} else if ("ccontent".toUpperCase().equals(name)) {
						bean.setCcontent(rs.getString(i + 1));
					} else if ("cVenBankMemo".toUpperCase().equals(name)) {
						bean.setcVenBankMemo(rs.getString(i + 1));
					} else if ("pk_vendor".toUpperCase().equals(name)) {
						bean.setPk_vendor(rs.getString(i + 1));
					} else if ("BANKACCOUNTNAME".toUpperCase().equals(name)) {
						bean.setBankaccountname(rs.getString(i + 1));
					} else if ("bank".toUpperCase().equals(name)) {
						bean.setBank(rs.getString(i + 1));
					} else if ("bankcode".toUpperCase().equals(name)) {
						bean.setBankcode(rs.getString(i + 1));
					} else if ("bankaccount".toUpperCase().equals(name)) {
						bean.setBankaccount(rs.getString(i + 1));
					} else if ("currency".toUpperCase().equals(name)) {
						bean.setCurrency(rs.getString(i + 1));
					}
					else if ("enterprise_type".toUpperCase().equals(name)) {
						bean.setQylb(rs.getString(i + 1));
					}else if ("registered_capital".toUpperCase().equals(name)) {
						bean.setZczj(rs.getString(i + 1));
					}else if ("establish_time".toUpperCase().equals(name)) {
						bean.setClsj(rs.getString(i + 1));
					}else if ("annual_sales".toUpperCase().equals(name)) {
						bean.setNxse(rs.getString(i + 1));
					}else if ("worker_number".toUpperCase().equals(name)) {
						bean.setZgrs(rs.getString(i + 1));
					}else if ("premises".toUpperCase().equals(name)) {
						bean.setJycs(rs.getString(i + 1));
					}else if ("cvenlperson".toUpperCase().equals(name)) {
						bean.setFrdb(rs.getString(i + 1));
					}else if ("legal_person_id_card".toUpperCase().equals(name)) {
						bean.setFrdbsfz(rs.getString(i + 1));
					}
					
				}

				JSONObject jo = new JSONObject();
				jo.put("success", true);

				jo.put("data", MyJSONConvert.toJSONStr(bean));
				result = jo.toString();

			} else {
				result = WARN_QUERY_INFO_NULL;
			}

		} catch (Exception e) {
			e.printStackTrace();
			log.error("查询供应商信息发生异常", e);
			result = ERROR_QUERY_INFO;
		} finally {
			DBUtil.close(rs, pst, conn);
		}
		return result;
	}

}
